﻿#region USING STATEMENTS
using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Krypton;
using Krypton.Lights;
using Fusion.Objects;
#endregion

#region CODE BODY
namespace Fusion.Management
{
    public static class FLights
    {
        //////////////////////////////////////////////////
        // Data Fields
        //////////////////////////////////////////////////
        private static KryptonEngine LightEngine;
        private static Texture2D LightTexture;
        private static Matrix Projection;
        private static int Width, Height;
        private static bool enabled;
        private static GameTime Time;

        //////////////////////////////////////////////////
        // Member Properties
        //////////////////////////////////////////////////
        public static bool Enabled
        {
            get { return enabled; }
            set { enabled = value; }
        }
        public static float Bluriness
        {
            get { return LightEngine.Bluriness; }
            set { LightEngine.Bluriness = value; }
        }
        public static Color AmbientColor
        {
            get { return LightEngine.AmbientColor; }
            set { LightEngine.AmbientColor = value; }
        }

        //////////////////////////////////////////////////
        // Public Static Methods
        //////////////////////////////////////////////////
        public static void InitializeLightEngine(GraphicsDevice Device, int BufferWidth, int BufferHeight, string Effect)
        {
            Projection = Matrix.CreateOrthographic(BufferWidth, BufferHeight, 0, 1);
            LightTexture = LightTextureBuilder.CreatePointLight(Device, 512);
            LightEngine = new KryptonEngine(Device, FContent.Content, Effect);
            LightEngine.Matrix = Projection;
            LightEngine.Initialize();
            Width = BufferWidth;
            Height = BufferHeight;
        }
        public static void UnloadLights()
        {
            LightEngine.UnloadContent();
        }
        public static void RemoveLight(ILight2D TheLight)
        {
            LightEngine.Lights.Remove(TheLight);
        }
        public static void RemoveHull(ShadowHull TheHull)
        {
            LightEngine.Hulls.Remove(TheHull);
        }
        public static void SetGameTime(GameTime UpdatedTime)
        {
            Time = UpdatedTime;
        }
        public static void Prepare()
        {
            if (Enabled)
                LightEngine.LightMapPrepare();
        }
        public static void RenderLights()
        {
            if (Enabled)
                LightEngine.Draw(Time);
        }
        public static ILight2D AddLight(float PosX, float PosY, float LightRange, float ShineIntensity, float ShineAngle, Color LightColor)
        {
            Light2D NewLight = new Light2D()
            {
                Position = new Vector2(PosX - (Width >> 1), -PosY + (Height >> 1)),
                Range = LightRange,
                Intensity = ShineIntensity,
                Color = LightColor,
                Angle = ShineAngle,
                Texture = LightTexture
            };

            LightEngine.Lights.Add(NewLight);
            return NewLight;
        }
        public static void AddLight(ILight2D NewLight)
        {
            if (!LightEngine.Lights.Contains(NewLight))
            {
                Light2D Light = ((Light2D)(NewLight));
                Light.Position = new Vector2(Light.Position.X - (Width >> 1), -Light.Position.Y + (Height >> 1));
                LightEngine.Lights.Add(Light);
            }
        }
        public static void AddHull(ShadowHull NewHull)
        {
            if (!LightEngine.Hulls.Contains(NewHull))
            {
                NewHull.Position = new Vector2(NewHull.Position.X - (Width >> 1), -NewHull.Position.Y + (Height >> 1));
                LightEngine.Hulls.Add(NewHull);
            }
        }
        public static void TurnLightOff(ILight2D TheLight)
        {
            ((Light2D)TheLight).IsOn = false;
        }
        public static void MoveLight(ILight2D TheLight, Vector2 NewPos)
        {
            ((Light2D)TheLight).Position = NewPos;
        }
        public static void ClearLights()
        {
            LightEngine.Lights.Clear();
            GC.Collect();
        }
        public static ShadowHull AddConvexHull(ref Vector2[] ConvexPoints)
        {
            Vector2[] Points = (Vector2[])ConvexPoints.Clone();
            for (int i = 0; i < Points.Length; ++i)
            {
                Points[i].X -= Width >> 1;
                Points[i].Y = -Points[i].Y + (Height >> 1);
            }

            LightEngine.Hulls.Add(ShadowHull.CreateConvex(ref Points));
            return LightEngine.Hulls[LightEngine.Hulls.Count - 1];
        }
        public static ShadowHull AddRectangleHull(Vector2 Position, Vector2 Size)
        {
            ShadowHull ToAdd = ShadowHull.CreateRectangle(Size);
            ToAdd.Position = new Vector2(Position.X - (Width >> 1), -Position.Y + (Height >> 1));
            LightEngine.Hulls.Add(ToAdd);
            return ToAdd;
        }
        public static ShadowHull AddCircleHull(Vector2 Position, float Radius, int Sides)
        {
            ShadowHull ToAdd = ShadowHull.CreateCircle(Radius, Sides);
            ToAdd.Position = new Vector2(Position.X - (Width >> 1), -Position.Y + (Height >> 1));
            LightEngine.Hulls.Add(ToAdd);
            return ToAdd;
        }
        public static void ClearHulls()
        {
            LightEngine.Hulls.Clear();
            GC.Collect();
        }
    }
}
#endregion

//////////////////////////////////////////////////////////////////////////////////////////////
// Copyright 2012 Jeffery Thomas Sventora
//////////////////////////////////////////////////////////////////////////////////////////////